package demoMod.invincibleOne.cards.invincible;

import basemod.helpers.BaseModCardTags;
import com.evacipated.cardcrawl.modthespire.Loader;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.unique.RemoveAllPowersAction;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.IntangiblePlayerPower;
import com.megacrit.cardcrawl.powers.StrengthPower;
import dLib.modcompat.ModManager;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.interfaces.IOnAddToMasterDeck;
import demoMod.invincibleOne.patches.PVPPatch;
import demoMod.invincibleOne.relics.TaskGoal;
import demoMod.invincibleOne.ui.BingoRewardItem;

import java.util.stream.Collectors;

public class InvincibleForm extends AbstractInvincibleCard implements IOnAddToMasterDeck {
    public static final String ID = InvincibleOneMod.makeID("InvincibleForm");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/InvincibleForm.png";

    private static final CardType TYPE = CardType.POWER;
    private static final CardRarity RARITY = CardRarity.RARE;
    private static final CardTarget TARGET = CardTarget.NONE;

    private static final int COST = 3;

    public InvincibleForm() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.tags.add(BaseModCardTags.FORM);
        if (Loader.isModLoaded(ModManager.TogetherInSpireMod.modId)) {
            this.rawDescription = PVPPatch.PVPModeCardDesc(ID);
            this.initializeDescription();
        }
        playFirst = true;
    }

    @Override
    public void onAddToMasterDeck() {
        InvincibleOneMod.bingoRewardList.getRewards().add(BingoRewardItem.legendaryCardRewardBuilder.build());
        TaskGoal tg = (TaskGoal) AbstractDungeon.player.getRelic(TaskGoal.ID);
        InvincibleOneMod.bingoRewardList.hide = false;
        tg.beginLongPulse();
        CardCrawlGame.sound.play("UNLOCK_PING");
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            this.upgradeBaseCost(0);
        };
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        if (Loader.isModLoaded(ModManager.TogetherInSpireMod.modId)) {
            if (PVPPatch.TryUseInvincibleForm()) {
                return;
            }
        }
        addToBot(new AbstractGameAction() {
            @Override
            public void update() {
                (AbstractDungeon.getCurrRoom()).cannotLose = false;
                for (AbstractMonster m2 : AbstractDungeon.getMonsters().monsters.stream().filter(m2 -> !m2.isDeadOrEscaped() || m2.halfDead).collect(Collectors.toList())) {
                    addToBot(new RemoveAllPowersAction(m2, false));
                    addToBot(new AbstractGameAction() {
                        @Override
                        public void update() {
                            m2.currentHealth = 0;
                            m2.healthBarUpdatedEvent();
                            m2.die();
                            isDone = true;
                        }
                    });
                }
                addToBot(new AbstractGameAction() {
                    @Override
                    public void update() {
                        AbstractDungeon.getCurrRoom().endBattle();
                    }
                });
                isDone = true;
            }
        });
    }
}
